home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Games Collection 1 / software vault.zip / software vault / CDR10 / XLIB06.ZIP / XPAL.ASM < prev    next >
Assembly Source File  |  1993-10-03  |  16KB  |  577 lines

  1. ;-----------------------------------------------------------------------
  2. ; MODULE XPAL
  3. ;
  4. ; Palette functions all MODE X 256 Color resolutions
  5. ;
  6. ; Compile with Tasm.
  7. ; C callable.
  8. ;
  9. ;
  10. ; ****** XLIB - Mode X graphics library                ****************
  11. ; ******                                               ****************
  12. ; ****** Written By Themie Gouthas                     ****************
  13. ;
  14. ; egg@dstos3.dsto.gov.au
  15. ; teg@bart.dsto.gov.au
  16. ;-----------------------------------------------------------------------
  17.  
  18.  
  19. COMMENT    $
  20.  
  21.  
  22.  
  23.     All the functions in this module operate on two variations of the
  24.     pallete buffer, the raw and annotated buffers.
  25.  
  26.     All those functions ending in buff operate on the following palette
  27.     structure:
  28.  
  29.        BYTE:r0,g0,b0,r1,g1,b1,...rn,gn,bn
  30.  
  31.     No reference to the starting colour index or number of colours stored
  32.     is contained in the structure.
  33.  
  34.     All those functions ending in struc operate on the following palette
  35.     structure:
  36.  
  37.        BYTE:c,BYTE:n,BYTE:r0,g0,b0,r1,g1,b1,...rn,gn,bn
  38.  
  39.     where c is the starting colour and n is the number of colours stored
  40.  
  41.  
  42.     NOTE: previously interrupts were disabled for DAC reads/writes but
  43.       they have been left enabled in this version to allow the mouse
  44.       interrupt to be invoked.
  45.  
  46. $
  47.  
  48.  
  49.  
  50.  
  51. include xlib.inc
  52. include xpal.inc
  53.  
  54. .code
  55.  
  56.  
  57. ;----------------------------------------------------------------------
  58. ; Read DAC palette into annotated type buffer with interrupts disabled
  59. ; ie BYTE colours to skip, BYTE colours to set, r1,g1,b1,r1,g2,b2...rn,gn,bn
  60. ;
  61. ; x_get_pal_struc(char far * pal, int num_colrs, int start_color)
  62. ;
  63. ; WARNING: memory for the palette buffers must all be pre-allocated
  64. ;
  65. ; Written by Themie Gouthas
  66. ;----------------------------------------------------------------------
  67. _x_get_pal_struc  proc
  68. ARG  PalBuff:dword,NumColors:word,StartColor:word
  69.      push  bp        ; Set up stack frame
  70.      mov   bp,sp
  71.      push  di
  72.      push  si
  73.      cld
  74.  
  75.      les   di,dword ptr [PalBuff]  ; Point es:di to palette buffer
  76.      mov   si,[StartColor]         ; Store the Start Colour
  77.      mov   ax,si
  78.      stosb
  79.      mov   dx,[NumColors]          ; Store the Number of Colours
  80.      mov   al,dl
  81.      stosb
  82.  
  83.      mov   cx,dx                   ; setup regs and jump
  84.      jmp   short ReadPalEntry
  85.  
  86. _x_get_pal_struc endp
  87.  
  88.  
  89.  
  90.  
  91.  
  92. ;----------------------------------------------------------------------
  93. ; Read DAC palette into raw buffer with interrupts disabled
  94. ; ie BYTE r1,g1,b1,r1,g2,b2...rn,gn,bn
  95. ;
  96. ; x_get_pal_raw(char far * pal, int num_colrs, int start_index)
  97. ;
  98. ; WARNING: memory for the palette buffers must all be pre-allocated
  99. ;
  100. ; Written by Themie Gouthas
  101. ;----------------------------------------------------------------------
  102. _x_get_pal_raw    proc
  103. ARG  PalBuff:dword,NumColors:word,StartColor:word
  104.      push  bp        ; Set up stack frame
  105.      mov   bp,sp
  106.      push  di
  107.      push  si
  108.  
  109.      les   di,dword ptr [PalBuff]  ; Point es:di to palette buffer
  110.  
  111.      mov  si,[StartColor]
  112.      mov  cx,[NumColors]
  113.  
  114. ReadPalEntry:
  115.      cld
  116.      WaitVsyncStart
  117.      mov  ax,si
  118.      mov  dx,DAC_READ_INDEX
  119.      ;cli
  120.      out  dx,al                    ; Tell DAC what colour to start reading
  121.      mov  dx,DAC_DATA
  122.  
  123.      mov  bx,cx                    ; set cx to Num Colors * 3 ( size of
  124.      shl  bx,1                     ; palette buffer)
  125.      add  cx,bx
  126.  
  127.      rep  insb                     ; read the palette enntries
  128.  
  129.      ;sti
  130.      pop  si
  131.      pop  di
  132.      pop  bp
  133.      ret
  134. _x_get_pal_raw endp
  135.  
  136.  
  137.  
  138. ;----------------------------------------------------------------------
  139. ; Write DAC palette from annotated type buffer with interrupts disabled
  140. ; ie BYTE colours to skip, BYTE colours to set, r1,g1,b1,r1,g2,b2...rn,gn,bn
  141. ;
  142. ; x_put_pal_struc(char far * pal)
  143. ;
  144. ; Written by Themie Gouthas
  145. ;----------------------------------------------------------------------
  146.  
  147. _x_put_pal_struc proc
  148. ARG     CompPalBuff:dword
  149.         push    bp      ;preserve caller's stack frame
  150.     mov     bp,sp   ;point to local stack frame
  151.     push    ds
  152.     push    si
  153.     cld
  154.     lds     si,[CompPalBuff]   ; load the source compressed colour data
  155.     lodsb               ; get the colours to skip
  156.     mov     ah,0
  157.     mov     bx,ax              ; skip colours
  158.  
  159.     lodsb               ; get the count of colours to set
  160.     mov     ah,0
  161.     mov    cx,ax              ; use it as a loop counter
  162.     jmp     short WritePalEntry
  163.  
  164. _x_put_pal_struc  endp
  165.  
  166.  
  167. ;----------------------------------------------------------------------
  168. ; Write DAC palette from annotated type buffer with interrupts disabled
  169. ; starting at a new palette index
  170. ;
  171. ; ie BYTE colours to skip, BYTE colours to set, r1,g1,b1,r1,g2,b2...rn,gn,bn
  172. ;
  173. ; x_transpose_pal_struc(char far * pal, int StartColor)
  174. ;
  175. ; WARNING: memory for the palette buffers must all be pre-allocated
  176. ;
  177. ; Written by Themie Gouthas
  178. ;----------------------------------------------------------------------
  179.  
  180. _x_transpose_pal_struc proc
  181. ARG     CompPalBuff:dword,StartColor:word
  182.         push    bp      ;preserve caller's stack frame
  183.     mov     bp,sp   ;point to local stack frame
  184.     push    ds
  185.     push    si
  186.     cld
  187.     lds     si,[CompPalBuff]   ; load the source compressed colour data
  188.     mov     bx,[StartColor]
  189.     mov     [si],bl
  190.     inc     si
  191.     lodsb               ; get the count of colours to set
  192.     mov     ah,0
  193.     mov    cx,ax              ; use it as a loop counter
  194.     jmp     short WritePalEntry
  195. _x_transpose_pal_struc endp
  196.  
  197.  
  198. ;----------------------------------------------------------------------
  199. ; Write DAC palette from raw buffer with interrupts disabled
  200. ; ie BYTE r1,g1,b1,r1,g2,b2...rn,gn,bn
  201. ;
  202. ; _x_put_pal_raw(char far * pal, int num_colrs, int start_index)
  203. ;
  204. ; Written by Themie Gouthas
  205. ;----------------------------------------------------------------------
  206. _x_put_pal_raw  proc
  207. ARG  PalBuff:dword,NumColors:word,StartColor:word
  208.         push bp        ; Set up stack frame
  209.         mov  bp,sp
  210.         push ds
  211.         push si
  212.  
  213.         mov  cx,[NumColors]      ; Number of colours to set
  214.         mov  bx,[StartColor]
  215.         lds  si,[PalBuff]        ; ds:si -> palette buffer
  216.  
  217.  
  218. WritePalEntry:
  219.                 mov  ax,@data
  220.         mov  es,ax
  221.         cmp  es:[_VsyncHandlerActive],TRUE
  222.         jne  @@NoVsyncHandler
  223. @@WaitForLast:
  224.         cmp  es:[_VsyncPaletteCount],0
  225.         jne  @@WaitForLast
  226.         push cx
  227.         push es
  228.         mov  di, offset _VsyncPaletteBuffer
  229.         mov  ax,3
  230.         mul  cx
  231.         mov  cx,ax
  232.         rep  movsb
  233.         pop  ds
  234.         pop  cx
  235.         mov  [_VsyncPaletteStart],bx
  236.         mov  [_VsyncPaletteCount],cx
  237.         jmp  short @@Done
  238. @@NoVsyncHandler:
  239.  
  240.  
  241.         or   cx,cx
  242.         jz   @@Done
  243.         ;cli
  244.         cld                      ; Make sure we're going the right way
  245.         WaitVsyncStart           ; Wait for vert sync to start
  246.                 mov  ax,bx
  247.         mov  bx,60               ; set the vsync check timer (Vsync
  248.                              ; is tested for at each bx'th entry to
  249.                      ; prevent snow 60 is otimum for 10
  250.                      ; MHz 286 or greater
  251.  
  252. @@SetLoop:
  253.         mov  dx,DAC_WRITE_INDEX  ; Tell DAC what colour index to start
  254.         out  dx,al               ; writing from
  255.         mov  dx,DAC_DATA
  256.  
  257.         outsb                    ; Set the red component
  258.         outsb                    ; Set the green component
  259.         outsb                    ; Set the blue component
  260.         inc  al                  ; increment the colour index
  261.         dec  bx                  ; decrement vsync test counter
  262.         js   @@test_vsync        ; ready to test for vsync again ?
  263.         loop @@SetLoop           ; No! - continue loop
  264.         jmp  short @@Done        ; All colours done
  265.  
  266. @@test_vsync:
  267.         mov     dx,INPUT_STATUS_0
  268.         push    ax               ; save current colour index
  269. @@Wait:
  270.         in      al,dx            ; wait for vsync leading edge pulse
  271.         test    al,08h
  272.         jz      @@Wait
  273.  
  274.         pop     ax               ; restore current colour index
  275.         mov     bx,60            ; reset vsync test counter
  276.         loop @@SetLoop           ; loop for next colour index
  277.  
  278. @@Done:
  279.         ;sti
  280.         pop  si
  281.         pop  ds
  282.         pop  bp
  283.         ret
  284. _x_put_pal_raw endp
  285.  
  286.  
  287.  
  288. ;----------------------------------------------------------------------
  289. ; Set the RGB setting of a vga color
  290. ;
  291. ; _x_set_rgb(unsigned char color, unsigned char R,unsigned char G,
  292. ;            unsigned char B)
  293. ;
  294. ;
  295. ; Written by Themie Gouthas
  296. ;----------------------------------------------------------------------
  297. _x_set_rgb  proc
  298. ARG  ColorIndex:byte,R:byte,G:byte,B:byte
  299.         push bp        ; Set up stack frame
  300.         mov  bp,sp
  301.  
  302.         mov  al,[ColorIndex]
  303.         mov  dx,DAC_WRITE_INDEX  ; Tell DAC what colour index to
  304.         out  dx,al               ; write to
  305.         mov  dx,DAC_DATA
  306.  
  307.         mov  al,[R]              ; Set the red component
  308.         out  dx,al
  309.         mov  al,[G]              ; Set the green component
  310.         out  dx,al
  311.         mov  al,[B]              ; Set the blue component
  312.         out  dx,al
  313.         pop  bp
  314.         ret
  315. _x_set_rgb endp
  316.  
  317.  
  318. ;----------------------------------------------------------------------
  319. ; Rotate annotated palette buffer entries
  320. ;
  321. ; x_rot_pal_struc(char far * pal, int direction)
  322. ;
  323. ; Direction : 0 = backward 1 = forward
  324. ;
  325. ; Written by Themie Gouthas
  326. ;----------------------------------------------------------------------
  327. _x_rot_pal_struc  proc
  328. ARG  PalBuff:dword,Direction:word
  329.     push bp        ; Set up stack frame
  330.     mov     bp,sp
  331.     push ds
  332.     push si
  333.     push di
  334.  
  335.     cld
  336.     lds     si,dword ptr [PalBuff]  ; point ds:si to Palette buffer
  337.     lodsw         ; al = colorst ot skip, ah = num colors
  338.  
  339.     xor  ch,ch    ; Set the number of palette entries to cycle in cx
  340.     mov  cl,ah    ;
  341.  
  342.     jmp  short RotatePalEntry
  343.  
  344. _x_rot_pal_struc  endp
  345.  
  346.  
  347.  
  348. ;----------------------------------------------------------------------
  349. ; Rotate raw palette buffer
  350. ;
  351. ; x_rot_pal_raw(char far * pal, int direction, int num_colrs)
  352. ;
  353. ; Direcction : 0 = backward 1 = forward
  354. ;
  355. ; Written by Themie Gouthas
  356. ;----------------------------------------------------------------------
  357. _x_rot_pal_raw    proc
  358. ARG  PalBuff:dword,Direction:word,NumColors:word
  359.     push bp        ; Set up stack frame
  360.     mov     bp,sp
  361.     push ds
  362.     push si
  363.     push di
  364.  
  365.     cld
  366.     mov     cx,[NumColors]          ; Set the number of palette entries to cycle
  367.     lds     si,dword ptr [PalBuff]  ; point ds:si to Palette buffer
  368.  
  369. RotatePalEntry:
  370.  
  371.  
  372.     mov     ax,ds                ; copy ds to es
  373.     mov     es,ax
  374.  
  375.     dec  cx
  376.     mov     bx,cx                ; Multiply cx by 3
  377.     shl     bx,1
  378.     add  cx,bx
  379.  
  380.     cmp  [Direction],0        ; are we going forward ?
  381.     jne  @@forward            ; yes - jump (colors move one position back)
  382.  
  383.     std                       ; no - set reverse direction
  384.     add  si,cx                ; set si to last byte in palette
  385.     add  si,2
  386.  
  387. @@forward:
  388.     mov     ax,si                ; copy si to di
  389.     mov     di,ax
  390.  
  391.     lodsb                     ; load first color triplet into regs
  392.     mov  dl,al
  393.     lodsb
  394.     mov  dh,al
  395.     lodsb
  396.     mov  bl,al
  397.  
  398.     rep     movsb                ; move remaining triplets direction indicated
  399.                   ; by direction flag
  400.  
  401.     mov  al,dl                ; write color triplet from regs to last position
  402.     stosb
  403.     mov  al,dh
  404.     stosb
  405.     mov  al,bl
  406.     stosb
  407.  
  408.     pop     di
  409.     pop     si
  410.     pop     ds
  411.     pop     bp
  412.     ret
  413. _x_rot_pal_raw  endp
  414.  
  415. ;----------------------------------------------------------------------
  416. ; Copy palette making intensity adjustment
  417. ; x_cpcontrast_pal_struc(char far *src_pal, char far *dest_pal, unsigned char Intensity)
  418. ;
  419. ; WARNING: memory for the palette buffers must all be pre-allocated
  420. ;
  421. ; Written by Themie Gouthas
  422. ;----------------------------------------------------------------------
  423. _x_cpcontrast_pal_struc    proc
  424. ARG  PalSrcBuff:dword,PalDestBuff:dword,Intensity:byte
  425.     push bp        ; Set up stack frame
  426.     mov     bp,sp
  427.     push ds
  428.     push si
  429.     push di
  430.  
  431.     cld
  432.     mov  bh,0ffh
  433.     sub  bh,[Intensity]
  434.     and  bh,07fh            ; Palettes are 7 bit
  435.     lds     si,dword ptr [PalSrcBuff]  ; point ds:si to Source Palette buffer
  436.     les     di,dword ptr [PalDestBuff] ; point ds:si to Source Palette buffer
  437.     lodsw                ; al = colorst ot skip, ah = num color
  438.     stosw
  439.  
  440.     xor  ch,ch    ; Set the number of palette entries to adjust
  441.     mov  cl,ah    ;
  442.  
  443.     mov  dx,0     ; flag set to 0 if all output palette entries zero
  444. @@MainLoop:
  445.     lodsw
  446.     sub  al,bh               ; adjust intensity and copy RED
  447.     jns  @@DecrementOK_R
  448.     xor  al,al
  449. @@DecrementOK_R:
  450.     sub  ah,bh               ; adjust intensity and copy GREEN
  451.     jns  @@DecrementOK_G
  452.     xor  ah,ah
  453. @@DecrementOK_G:
  454.     or   dx,ax
  455.     or   dl,ah
  456.     stosw
  457.     lodsb
  458.     sub  al,bh               ; adjust intensity and copy BLUE
  459.     jns  @@DecrementOK_B
  460.     xor  al,al
  461. @@DecrementOK_B:
  462.     or   dl,al
  463.     stosb
  464.     loop @@MainLoop
  465.  
  466.     mov  ax,dx
  467.     pop  di
  468.     pop     si
  469.     pop     ds
  470.     pop     bp
  471.     ret
  472. _x_cpcontrast_pal_struc  endp
  473.  
  474.  
  475.  
  476. ;----------------------------------------------------------------------
  477. ; Write DAC palette from annotated type buffer with specified intensity
  478. ; ie BYTE colours to skip, BYTE colours to set, r1,g1,b1,r1,g2,b2...rn,gn,bn
  479. ;
  480. ; x_put_contrast_pal_struc(char far * pal, unsigned char  intensity)
  481. ;
  482. ; Designed for fading in or out a palette without using an intermediate
  483. ; working palette buffer ! (Slow but memory efficient ... OK for small
  484. ; pal strucs}
  485. ;
  486. ; Written by Themie Gouthas
  487. ;----------------------------------------------------------------------
  488.  
  489. _x_put_contrast_pal_struc proc
  490. ARG     CompPalBuff:dword,Intensity:byte
  491.         push    bp      ;preserve caller's stack frame
  492.     mov     bp,sp   ;point to local stack frame
  493.     push    ds
  494.     push    si
  495.     push    di
  496.     cld
  497.  
  498.     mov     bh,0ffh
  499.     sub     bh,[Intensity]
  500.     and     bh,07fh            ; Palettes are 7 bit
  501.     mov     di,40              ; set the vsync check timer (Vsync
  502.                    ; is tested for at each di'th entry to
  503.                    ; prevent snow 40 is otimum for 10
  504.                    ; MHz 286 or greater)
  505.     lds     si,[CompPalBuff]   ; load the source compressed colour data
  506.     lodsb               ; get the colours to skip
  507.     mov     bl,al
  508.  
  509.     lodsb               ; get the count of colours to set
  510.     mov     ah,0
  511.     mov    cx,ax              ; use it as a loop counter
  512.     or      cx,cx
  513.     jz      @@Done
  514.  
  515.     WaitVsyncStart           ; Wait for vert sync to start
  516.  
  517. @@MainLoop:
  518.         mov  al,bl
  519.     mov  dx,DAC_WRITE_INDEX  ; Tell DAC what colour index to start
  520.     out  dx,al               ; writing from
  521.     inc  dx                  ; == mov  dx,DAC_DATA
  522.  
  523.     lodsb                    ; Load each colour component, modify for
  524.     sub  al,bh               ; intensity and write to DAC H/Ware
  525.     jns  @@DecrementOK_R
  526.     xor  al,al
  527. @@DecrementOK_R:
  528.     out  dx,al
  529.  
  530.     lodsb
  531.     sub  al,bh
  532.     jns  @@DecrementOK_G
  533.     xor  al,al
  534. @@DecrementOK_G:
  535.     out  dx,al
  536.  
  537.     lodsb
  538.     sub  al,bh
  539.     jns  @@DecrementOK_B
  540.     xor  al,al
  541. @@DecrementOK_B:
  542.     out  dx,al
  543.  
  544.     inc  bl                  ; increment color index
  545.     dec  di                  ; decrement vsync test flag
  546.     js   @@test_vsync
  547.     loop @@MainLoop
  548.     jmp  short @@Done
  549.  
  550.  
  551. @@test_vsync:
  552.     mov     dx,INPUT_STATUS_0
  553.     push    ax               ; save current colour index
  554. @@Wait:
  555.     in      al,dx            ; wait for vsync leading edge pulse
  556.     test    al,08h
  557.     jz      @@Wait
  558.  
  559.     pop     ax               ; restore current colour index
  560.     mov     di,40            ; reset vsync test counter
  561.     loop    @@MainLoop       ; loop for next colour index
  562.  
  563. @@Done:
  564.     ;sti
  565.     pop  di
  566.     pop  si
  567.     pop  ds
  568.     pop  bp
  569.     ret
  570.  
  571. _x_put_contrast_pal_struc   endp
  572.  
  573.  
  574.     end
  575.  
  576.  
  577.